package com.bl.student.api.controller.file;

import com.bl.student.api.controller.BaseController;
import com.bl.student.common.ApiResponseBody;
import com.bl.student.service.biz.file.FileService;
import com.bl.student.service.vo.file.FileInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by liangweikang on 18/1/28.
 */
@RestController
@Log4j2
@RequestMapping(value = {"/lian/manager/file"})
@Api(value = "文件上传相关api", description = "文件上传相关api")
public class FileController extends BaseController {

    @Autowired
    FileService fileService;

    @ApiOperation(value = "上传文件api", notes = "通过multipart/form-data的方式上传，返回文件")
    @ApiImplicitParams({
    })
    @RequestMapping(value = {"/upload"}, method = {POST})
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ApiResponseBody upload(
            @ApiParam(required = true, value = "文件")
            @RequestParam(value = "file", required = true) MultipartFile file,
            HttpServletRequest request) {

        String fileName = file.getOriginalFilename();
        System.out.println(fileName);

        FileInfoVo imgInfoVo = fileService.upload(file, "lian");

        return ApiResponseBody.createSuccessBody(imgInfoVo);
    }
}
